//Jonathan Flessner
//27-April-2014
//gainCard function

#include <stdio.h>
#include <stdlib.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"

void printVars(int card, int whereTo, int p, struct gameState *gS, int hCount, int dkCount, int dCount, int sCount){
	printf("Card = %d\n", card);
	printf("Card sent to %d\n", whereTo);
	printf("Player = %d\n", p);
	printf("Hand count = %d\n", gS->handCount[p]);
	printf("Expected hand count = %d\n", hCount + 1);
	printf("Deck count = %d\n", gS->deckCount[p]);
	printf("Expected deck count = %d\n", dkCount + 1);
	printf("Discard count = %d\n", gS->discardCount[p]);
	printf("Expected discard count = %d\n", dCount + 1);
	printf("Supply count = %d\n", gS->supplyCount[p]);
	printf("Expected supply count = %d\n", sCount + 1);
	//exit(EXIT_FAILURE);
}

int main(){
	srand(9);
	struct gameState *gS = malloc(sizeof(struct gameState));
	int numPlayers = 2;
	int testNum = 1000;
	int i = 0;
	int card = 0;
	int whereTo = 0;
	int p = 0;
	int dCount = 0;
	int dkCount = 0;
	int hCount = 0;
	int sCount = 0;
	for(i = 0; i <= testNum; i++){
		p  = rand() % numPlayers;						//random player to test
		card = rand() % 27;								//random card
		whereTo = rand() % 3;							//random location to send card (discard, deck, hand)
		gS->handCount[p] = rand() % MAX_HAND + 1;		//random num in hand
		hCount = gS->handCount[p];
		gS->deckCount[p] = rand() % MAX_DECK + 1;		//random num in deck
		dkCount = gS->deckCount[p];
		gS->discardCount[p] = rand() % MAX_DECK + 1;	//random num in discard
		dCount = gS->discardCount[p];
		gS->supplyCount[card] = rand() % MAX_DECK + 1;	//random supply
		sCount = gS->supplyCount[card];
		
		gainCard(card, gS, whereTo, p);					//gain card
		//run tests
		if(whereTo == 0){
			if(dCount + 1 == gS->discardCount[p]){
				//printf("Pass. Card added to discard.\n");
			}
			else{
				printf("Fail. Discard count unexpected.\n");
				printVars(card, whereTo, p, gS, hCount, dkCount, dCount, sCount);
			}
		}
		else if(whereTo == 1){
			if(dkCount + 1 == gS->deckCount[p]){
				//printf("Pass. Card added to discard.\n");
			}
			else{
				printf("Fail. Deck count unexpected.\n");
				printVars(card, whereTo, p, gS, hCount, dkCount, dCount, sCount);
			}
		}
		else if(whereTo == 2){
			if(hCount + 1 == gS->handCount[p]){
				//printf("Pass. Card added to discard.\n");
			}
			else{
				printf("Fail. Hand count unexpected.\n");
				printVars(card, whereTo, p, gS, hCount, dkCount, dCount, sCount);
			}
		}
		else{
			printf("Test Error.\n");
			printVars(card, whereTo, p, gS, hCount, dkCount, dCount, sCount);
		}
		if(sCount - 1 == gS->supplyCount[card]){
			//printf("Pass. Card supply decremented\n");
		}
		else{
			printf("Fail. Supply count unexpected.\n");
			printVars(card, whereTo, p, gS, hCount, dkCount, dCount, sCount);
		}
	}
	free(gS);
	printf("Tests completed: %d. Failed tests dumped variables.\n", testNum);	
	return 0;
}